package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Query;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.MaterialHDaoCustom;
import com.wits.qso.entity.Course;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.entity.MaterialH;


@Component
public class MaterialHDaoImpl extends BaseDaoImpl<MaterialH, String> implements MaterialHDaoCustom
{
	protected static final Logger logger = LoggerFactory.getLogger( MaterialHDaoImpl.class );
	
	public class Material
	{
		private String id = null;
		private String couName = null;
		private String title = null;
		private String userId = null;

		public Material() { super(); }

		public Material(String id, String couName, String userId, String title) {
			this.id = id;
			this.couName = couName;
			this.title = title;
			this.userId = userId;
		}

		public String getId() { return id; }

		public String getCouName() { return couName; }

		public String getTitle() { return title; }

		public String getUserId() { return userId; }

		public void setId(String id) { this.id = id; }

		public void setCouName(String couName) { this.couName = couName; }

		public void setTitle(String title) { this.title = title; }

		public void setUserId(String userId) { this.userId = userId; }
		
		@Override
		public String toString(){
			return JSONObject.fromObject(this, MaterialD.getBaseJsonConfig()).toString();
		}
	}
	
	@Override
	public List<Material> findMaterials(String id, String couName, String title) {
		StringBuilder builder = new StringBuilder( "SELECT h.id, h.couid, h.userId, d.title FROM MaterialH h, MaterialD d WHERE d.mtrhId = h.id" );
		List<Material> result = new ArrayList<Material>();

		if (StringUtils.isNotBlank( id )) {
			id = "%" + id + "%";
			builder.append( " AND h.id LIKE :id " );
		}
		
		if (StringUtils.isNotBlank( couName )) {
			couName = "%" + couName + "%";
			builder.append( " AND h.couid.title LIKE :couName " );
		}
		
		if (StringUtils.isNotBlank( title )) {
			title = "%" + title + "%";
			builder.append( " AND d.title LIKE :title " );
		}
		
		builder.append( " ORDER BY h.id ASC " );
		
		Query query = em.createQuery( builder.toString() );
		
		if (StringUtils.isNotBlank( id )) { query.setParameter( "id", id ); }
		
		if (StringUtils.isNotBlank( couName )) { query.setParameter( "couName", couName ); }
		
		if (StringUtils.isNotBlank( title )) { query.setParameter( "title", title ); }

		List queryResult = query.getResultList();
		if (!queryResult.isEmpty()) {
			Iterator iterator = queryResult.iterator();
			while (iterator.hasNext()) {
				Object[] values = (Object[]) iterator.next();
				result.add( new Material( (String) values[0], ((Course) values[1]).getTitle(), (String) values[2], (String) values[3] ) );
			}
		}

		return result;
	}

	@Override
	public MaterialH findByName(String name) {
		String hql="from MaterialH h where h.couid.title='"+name+"'";
		Query query = em.createQuery( hql );
		return (MaterialH) query.getSingleResult();
	}
}
